package com.zwd.deep.controller;

import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
import com.zwd.deep.common.RequestResult;
import com.zwd.deep.entity.dto.DiagnosisDto;
import com.zwd.deep.entity.vo.TaskVo;
import com.zwd.deep.service.TaskService;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.model.Generation;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * AI诊断
 *
 * @author zwd
 */
@RestController
@RequestMapping("/ai")
@AllArgsConstructor
@Slf4j
@Validated
public class AiController {


    private final TaskService aiService;
    private final DashScopeChatModel chatModel;

    @GetMapping("/simple/chat")
    public Generation simpleChat(String prompt) {
        ChatResponse chatResponse = chatModel.call(new Prompt(prompt));
        return chatResponse.getResult();
    }

    /**
     * AI建议
     *
     * @param aiDiagnoseDto AI诊断参数
     * @return 诊断结果
     */
    @PutMapping("/diagnose")
    public Generation aiDiagnose(@Validated @RequestBody DiagnosisDto aiDiagnoseDto) {
        log.info("aiDiagnose aiDiagnoseDto: {}", aiDiagnoseDto);
        String prompt = "你现在是一名资深的内科医生，以下是一次诊断的记录内容，请给出评价与改进建议，简短一点";
        ChatResponse call = chatModel.call(new Prompt(prompt + aiDiagnoseDto.toString()));
        String string = call.getResult().toString();
        log.info("aiDiagnose result: {}", string);
        return call.getResult();
    }

    /**
     * AI诊断肺部CT图片
     *
     * @param file ct
     * @return 诊断结果
     */
    @PostMapping("/diagnose")
    public RequestResult<String> aiDiagnose(@NotNull(message = "图片不能为空") @RequestBody MultipartFile file) {
        String result = aiService.check(file);
        return RequestResult.success(result);
    }
    @RequestMapping("/history")
    public RequestResult<String> history() {
        return RequestResult.success("history");
    }

}
